package cn.edu.sdjzu.xg.bysj.dao;

import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisor;
import cn.edu.sdjzu.xg.bysj.domain.ApplicationForSupervisorEntry;
import com.google.protobuf.BoolValueOrBuilder;
import lombok.extern.slf4j.Slf4j;
import util.JdbcHelper;

import java.sql.*;
import java.util.Collection;

@Slf4j
public class ApplicationForSupervisorDao {
    //新建ApplicationForSupervisorDao对象
    private static ApplicationForSupervisorDao applicationForSupervisorDao = new ApplicationForSupervisorDao();
    //构造器
    private ApplicationForSupervisorDao(){}
    //获取实例
    public static ApplicationForSupervisorDao getInstance(){
        return applicationForSupervisorDao;
    }

    /**
     * 删除后添加（可以更新，可以添加）
     * @param connection
     * @param applicationForSupervisor
     * @return
     * @throws SQLException
     */
    public boolean add (Connection connection , ApplicationForSupervisor applicationForSupervisor) throws SQLException {
        //删除当前学生现存的志愿信息
        this.delete(connection,applicationForSupervisor);
        //添加志愿
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO ApplicationForSupervisor " + " (selfIntroduction,student_id,year) VALUES (?,?,?)", Statement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1,applicationForSupervisor.getSelfIntroduction());
        preparedStatement.setInt(2,applicationForSupervisor.getStudent().getId());
        preparedStatement.setInt(3,applicationForSupervisor.getYear());
        int rowAffected = preparedStatement.executeUpdate();
        //添加失败返回false
        if (rowAffected <= 0){
            return false;
        }
        int idOfAddedApplication = 0;
        //获得生成的主键的集合
        ResultSet resultSet = preparedStatement.getGeneratedKeys();
        if (resultSet.next()){
            //读取第一个主键的值为新添加的id
            idOfAddedApplication = resultSet.getInt(1);
        }
        log.trace("添加的id" + idOfAddedApplication);
        JdbcHelper.close(resultSet,preparedStatement);
        //接下来添加志愿条目
        Collection<ApplicationForSupervisorEntry> applicationForSupervisorEntries =
                applicationForSupervisor.getApplicationForSupervisorEntries();
        log.trace(applicationForSupervisorEntries.toString());

        //把志愿条目集合里的所有志愿条目信息添加进表中
        for (ApplicationForSupervisorEntry applicationForSupervisorEntry : applicationForSupervisorEntries){
            log.trace(applicationForSupervisorEntry.toString());
            preparedStatement = connection.prepareStatement("INSERT INTO ApplicationForSupervisorEntry " +
                    "(teacher_id,applicationForSupervisor_id,priority)VALUE (?,?,?)");
            preparedStatement.setInt(1,applicationForSupervisorEntry.getTeacher().getId());
            preparedStatement.setInt(2,idOfAddedApplication);
            preparedStatement.setInt(3,applicationForSupervisorEntry.getPriority());
            int entry_RowAffected = preparedStatement.executeUpdate();
            if (entry_RowAffected <= 0 ){
                return false;
            }
            JdbcHelper.close(null,preparedStatement);
        }
        return true;
    }

    /**
     *当前学生的选择志愿的信息
     * @param connection
     * @param applicationForSupervisor
     * @return
     * @throws SQLException
     */
    private boolean delete(Connection connection ,ApplicationForSupervisor applicationForSupervisor) throws SQLException {
        int student_id = applicationForSupervisor.getStudent().getId();
        String delete_application = "DELETE FROM ApplicationForSupervisor WHERE student_id = " + student_id;
        PreparedStatement preparedStatement = connection.prepareStatement(delete_application);
        int rowAffected = preparedStatement.executeUpdate();
        JdbcHelper.close(null,preparedStatement);
        return rowAffected>0;
    }
}
